package com.jmz.apt.api.Provider;

import com.jmz.apt.api.Utils.MD5Util;
import com.jmz.apt.api.entity.IEntity;
import com.jmz.apt.api.entity.dbEntity.JmzAdminUser;
import com.jmz.apt.api.message.request.Condition;
import com.jmz.apt.api.message.request.Query;
import com.jmz.apt.api.message.request.RequestMessage;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * Created by fanglexue on 2019/10/28.
 */
public class AdminUserProvider extends BaseProvider {

    public String getCount(List<Condition> conditions) {
        return super.count(conditions);
    }

    public String select(String columns, List<Condition> conditions, Query query) {
        return  super.findAll(columns,conditions,query);
    }

    public String getTableName(){
        return "jmz_admin_user";
    }


    public String insert(JmzAdminUser jmzAdminUser) {
        SQL sql = new SQL();
        sql.INSERT_INTO(getTableName());
        if (jmzAdminUser.getNickName() != null) {
            sql.VALUES("name", "#{name}");
        }

        if (jmzAdminUser.getName() != null) {
            sql.VALUES("name", "#{name}");
        }

        if (jmzAdminUser.getMobile() != null) {
            sql.VALUES("mobile", "#{mobile}");
        }

        if (jmzAdminUser.getEmail() != null) {
            sql.VALUES("email", "#{email}");
        }

        if (jmzAdminUser.getPassword() != null) {
            jmzAdminUser.setPassword(MD5Util.encode(jmzAdminUser.getPassword()));
            sql.VALUES("password", "#{password}");
        }
        if (jmzAdminUser.getAvatar() != null) {
            sql.VALUES("avatar", "#{avatar}");
        }
        return sql.toString();
    }

    public String selectRepeatOne(JmzAdminUser jmzAdminUser) {
        SQL sql = new SQL();
        sql.SELECT("*");
        sql.FROM(getTableName());
        if (jmzAdminUser.getName() != null) {
            sql.WHERE("name=#{name}");
            sql.OR();
        }
        if (jmzAdminUser.getId() != null) {
            sql.WHERE("id=#{id}");
            sql.OR();
        }
        if (jmzAdminUser.getEmail() != null) {
            sql.WHERE("email=#{email}");
            sql.OR();
        }
        if (jmzAdminUser.getMobile() != null) {
            sql.WHERE("mobile=#{mobile}");
        }

        String lastSql = sql.toString();
        System.out.println(lastSql);
        return lastSql + "limit 1";
    }


    public String selectOne(JmzAdminUser jmzAdminUser) {
        SQL sql = new SQL();
        sql.SELECT("*");
        sql.FROM(getTableName());
        if (jmzAdminUser.getName() != null) {
            sql.WHERE("name=#{name}");
        }
        if (jmzAdminUser.getId() != null) {
            sql.WHERE("id=#{id}");
        }
        if (jmzAdminUser.getEmail() != null) {
            sql.WHERE("email=#{email}");
        }
        if (jmzAdminUser.getMobile() != null) {
            sql.WHERE("mobile=#{mobile}");
        }
        if (jmzAdminUser.getPassword() != null) {
            jmzAdminUser.setPassword(MD5Util.encode(jmzAdminUser.getPassword()));
            sql.WHERE("password=#{password}");
        }

        String lastSql = sql.toString();
        System.out.println(lastSql);
        return lastSql + "limit 1";
    }


    public String delete(JmzAdminUser jmzAdminUser) {
        return new SQL() {{
            DELETE_FROM(getTableName());
            WHERE("id = #{id}"); //暂时禁止批量删除
        }}.toString();
    }

    public String update(JmzAdminUser jmzAdminUser) {
        return new SQL() {{
            UPDATE(getTableName());
            if (jmzAdminUser.getId() != null) {
                SET("id = #{id}");
            }
            if (jmzAdminUser.getName() != null) {
                SET("name = #{name}");
            }
            if (jmzAdminUser.getEmail() != null) {
                SET("email=#{email}");
            }
            if (jmzAdminUser.getMobile() != null) {
                SET("mobile=#{mobile}");
            }
            if (jmzAdminUser.getPassword() != null) {
                jmzAdminUser.setPassword(MD5Util.encode(jmzAdminUser.getPassword()));
                SET("password = #{password}");
            }
            WHERE("id = #{id}");
        }}.toString();
    }
}
